﻿function messageloop() {
    this.__registerMessage = new HashTable();

    this.register = function (receiveObj, messageType, receiverHandler) {
        if (typeof (receiveObj) != "undefined" && typeof (receiverHandler) != "undefined") {
            var receiveObjArray;
            if (this.__registerMessage.contains(messageType)) {
                receiveObjArray = this.__registerMessage.items(messageType);
                receiveObjArray.push(receiveObj);
                this.__registerMessage.Remove(messageType);
            }
            else {
                receiveObjArray = new Array();
                receiveObjArray.push(receiveObj);
            }
            this.__registerMessage.Add(messageType, receiveObjArray);
            receiveObj.bind(messageType, function (event, msg) {
                receiverHandler.onReceiver(msg);
            });
        }
        else {
            alert("receiveObj undefined");
        }
    };

    this.put = function (messageObj) {

        var receiveObjArray = this.__registerMessage.items(messageObj.getMsgtype());
        if (typeof (receiveObjArray) != "undefined") {
            for (var i = 0; i < receiveObjArray.length; i++) {
                var tmpObj = receiveObjArray[i];
                tmpObj.trigger(messageObj.getMsgtype(), messageObj);
            }
        }
    };
}